//
//  WXWelcomeViewController.swift
//  weibo
//
//  Created by 王旭 on 16/4/19.
//  Copyright © 2016年 王旭. All rights reserved.
//

import UIKit
import SDWebImage

class WXWelcomeViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor(white: 247/255.0, alpha: 1)
        setupUI()
    }
    
    func setupUI() {
        //添加控件
        view.addSubview(iconImageView)
        view.addSubview(textLabel)
        //做约束
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(200)
            make.centerX.equalTo(view)
            make.size.equalTo(CGSizeMake(90, 90))
            
        textLabel.snp_makeConstraints(closure: { (make) -> Void in
            make.centerX.equalTo(iconImageView)
            make.top.equalTo(iconImageView.snp_bottom).offset(15)
        })
        }
    }
    
    //约束动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        //变更约束
        iconImageView.snp_updateConstraints{ (make) -> Void in
            make.top.equalTo(100)
        }
        //执行动画
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(1, animations: { () -> Void in
                    self.textLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        print("在欢迎页面控制器,即将跳转跳转控制器")
                        
//                        let applicationDelegate = UIApplication.sharedApplication().delegate as? AppDelegate
//                        applicationDelegate?.window?.rootViewController = WXMainViewController()
                        
                        NSNotificationCenter.defaultCenter().postNotificationName(WXChangeRootVCNotification, object: self)
                })
        }
        
        
    }

    
    //懒加载
    //iconImage
    private lazy var iconImageView : UIImageView = {()->UIImageView in
        let iconImageView = UIImageView(image: UIImage(named:"avatar_default_big"))
        //切圆角
        iconImageView.layer.cornerRadius = 45
//        iconImageView.clipsToBounds = true
        iconImageView.layer.masksToBounds = true
        
        //判断是否有头像,有就加载头像
        if let viewModel = WXUserAccountViewModel.sharedUserAccountViewModel.userAccount?.avatar_large {
            iconImageView.sd_setImageWithURL(NSURL(string: viewModel),placeholderImage: UIImage(named: "avatar_default_big"))
        }
        return iconImageView
    }()
    //textLabel
    private lazy var textLabel :UILabel = {
        let textLabel = UILabel()
        textLabel.textColor = UIColor.darkGrayColor()
        textLabel.text = "欢迎回来"
        textLabel.alpha = 0
        return textLabel
    }()
}
